package com.zysc.processflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zysc.processflow.common.util.StringUtils;
import com.zysc.processflow.entity.Personorg;
import com.zysc.processflow.mapper.PersonorgMapper;
import com.zysc.processflow.service.IPersonorgService;
import com.zysc.processflow.common.base.BaseService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 人员单位关系表 服务实现类
 * </p>
 *
 * @author 正元智慧城市_
 * @since 2023-01-06
 */
@Service
public class PersonorgServiceImpl extends BaseService<PersonorgMapper, Personorg> implements IPersonorgService {


    @Override
    public Boolean DeletePersonorg(Personorg entity) {
        boolean b = true;
        String personid = entity.getPersonid();
        if(StringUtils.isNull(personid)){
            throw new RuntimeException("缺少人员唯一标识符");
        }
        String orgid = entity.getOrgid();
        List<Personorg> list = new ArrayList<>();
        LambdaQueryWrapper<Personorg> qw = new LambdaQueryWrapper<>();
        qw.eq(Personorg::getPersonid,personid)
             .eq(!StringUtils.isNull(orgid),Personorg::getOrgid,orgid);
        list = this.list(qw);
        if(list.size()>0){
            b = this.remove(qw);
        }
        return b;
    }

    @Override
    public List<Personorg> getpersonorgListByPersonidAndOrgid(String personid, String orgid) {
        List<Personorg> list = new ArrayList<>();
        LambdaQueryWrapper<Personorg> qw = new LambdaQueryWrapper<>();
        qw.eq(!StringUtils.isNull(personid),Personorg::getPersonid,personid)
                .eq(!StringUtils.isNull(orgid),Personorg::getOrgid,orgid);
        list = this.list(qw);
        return list;
    }

    @Override
    public Boolean saveOrUpdatePersonorgInfo(Personorg entity) {
        boolean b = true;
        String personid = entity.getPersonid();
        String orgid = entity.getOrgid();
        if(StringUtils.isNull(personid) || StringUtils.isNull(orgid)){
            throw new RuntimeException("缺少人员或者单位数据");
        }
        List<Personorg> list = this.getpersonorgListByPersonidAndOrgid(personid,orgid);
        if(list.size()>0){
            entity.setId(list.get(0).getId());
        }
        b = this.saveOrUpdate(entity);
        return b;
    }

}
